namespace SimpleTPL;

using System;
using System.Runtime.CompilerServices;

public struct SimpleTaskAwaiter<TResult> : INotifyCompletion
{
    private readonly SimpleTask<TResult> _task;

    public SimpleTaskAwaiter(SimpleTask<TResult> task)
    {
        _task = task;
    }

    public bool IsCompleted => _task.IsCompleted;

    public void OnCompleted(Action continuation)
    {
        _task.ContinueWith(_ => continuation());
    }

    public TResult GetResult()
    {
        if (_task._exception != null)
        {
            throw _task._exception;
        }
        return _task.Result;
    }
}